{php include wl_template('common/header');}
<ul class="nav nav-tabs">
	<li {if $operation == 'display'} class="active" {/if}><a href="{php echo web_url('goods/coupon',array('op' =>'display'))}">优惠券列表</a></li>
	<li{if empty($coupon['id']) && $operation == 'on_shelves'} class="active" {/if}><a href="{php echo web_url('goods/coupon',array('op' =>'on_shelves'))}">上架的优惠券</a></li>
	<li{if empty($coupon['id']) && $operation == 'off_shelves'} class="active" {/if}><a href="{php echo web_url('goods/coupon',array('op' =>'off_shelves'))}">下架的优惠券</a></li>
	<li{if empty($coupon['id']) && $operation == 'post'} class="active" {/if}><a href="{php echo web_url('goods/coupon',array('op' =>'post'))}">添加优惠券</a></li>
	{if !empty($coupon['id']) &&  $operation == 'post'}<li class="active"><a href="{php echo web_url('goods/coupon',array('op' =>'post','id'=>$coupon['id']))}">编辑优惠券</a></li>{/if}
</ul>

{if $operation == 'display'}
<div class="main panel panel-default">
	<div class="panel-body table-responsive">
		<table class="table table-hover">
			<thead class="navbar-inner">
			<tr>
				<th style="width:5%;">ID</th>
				<th style="width:15%;">图片</th>
				<th style="width:35%;">优惠券名称</th>
				<th style="width:20%;">优惠券小标题</th>
				<th style="width:15%;">适用人群</th>
				<th class="text-center" style="width:10%;">操作</th>
			</tr>
			</thead>
			<tbody>
			{loop $list $coupon}
			<tr>
				<td>{$coupon['id']}</td>
				<td>
					<img width='45' height="45" src="{IMAGE_PIXEL}" data-url="{php echo tomedia($coupon['pic']);}" class="scrollLoading" onerror="this.src='{IMAGE_NOPIC_SMALL}'" />
				</td>
				<td>{$coupon['name']}</td>
				<td>{$coupon['subtitle']}</td>
				<td>{$coupon['person']}</td>
				<td class="text-right">
					<a href="{php echo web_url('goods/coupon', array('op' => 'post', 'id' => $coupon['id']))}" class="btn btn-default btn-sm" data-toggle="tooltip" data-placement="top" title="修改"><i class="fa fa-edit"></i></a>
					<a href="javascript:;" c-id="{$coupon['id']}" class="btn btn-default btn-sm js-delete" data-toggle="tooltip" data-placement="top" title="删除"><i class="fa fa-times"></i></a><br/>
					{if $coupon['isshow'] == 1}
					<a href="javascript:;" class="js-off-shelves-goods btn btn-default" goods-id="{$coupon['id']}">下架</a>&nbsp;&nbsp;
					{else}
					<a href="javascript:;" class="js-on-shelves-goods btn btn-default" goods-id="{$coupon['id']}">上架</a>
					{/if}
				</td>
			</tr>
			{/loop}
			<!--<tr>-->
			<!--<td colspan="5" style="text-align: right;"><a href="javascript:;" class="btn btn-success min-width js-batch js-off-shelves">下架</a></td>-->
			<!--<td><a href="javascript:;" class="btn btn-primary min-width js-batch js-on-shelves">上架</a></td>-->
			<!--</tr>-->
			{if !empty($coupon)}
				{if !empty($pager)}
				<tr>
					<td colspan="6">{$pager}</td>
				</tr>
				{/if}
			{else}
				<tr>
					<td colspan="6" style="text-align: center;"><span style="color:red;font-size: 18px;">暂无记录</span></td>
				</tr>
			{/if}
			</tbody>
		</table>
	</div>
</div>
{elseif $operation == 'on_shelves'}
<div class="main panel panel-default">
	<div class="panel-body table-responsive">
		<table class="table table-hover">
			<thead class="navbar-inner">
			<tr>
				<th style="width:5%;">ID</th>
				<th style="width:15%;">图片</th>
				<th style="width:35%;">优惠券名称</th>
				<th style="width:20%;">优惠券小标题</th>
				<th style="width:15%;">适用人群</th>
				<th class="text-center" style="width:10%;">操作</th>
			</tr>
			</thead>
			<tbody>
			{loop $list $coupon}
			<tr>
				<td>{$coupon['id']}</td>
				<td>
					<img width='45' height="45" src="{IMAGE_PIXEL}" data-url="{php echo tomedia($coupon['pic']);}" class="scrollLoading" onerror="this.src='{IMAGE_NOPIC_SMALL}'" />
				</td>
				<td>{$coupon['name']}</td>
				<td>{$coupon['subtitle']}</td>
				<td>{$coupon['person']}</td>
				<td class="text-right">
					<a href="{php echo web_url('goods/coupon', array('op' => 'post', 'id' => $coupon['id']))}" class="btn btn-default btn-sm" data-toggle="tooltip" data-placement="top" title="修改"><i class="fa fa-edit"></i></a>
					<a href="javascript:;" c-id="{$coupon['id']}" class="btn btn-default btn-sm js-delete" data-toggle="tooltip" data-placement="top" title="删除"><i class="fa fa-times"></i></a><br/>
					{if $coupon['isshow'] == 1}
					<a href="javascript:;" class="js-off-shelves-goods btn btn-default" goods-id="{$coupon['id']}">下架</a>&nbsp;&nbsp;
					{else}
					<a href="javascript:;" class="js-on-shelves-goods btn btn-default" goods-id="{$coupon['id']}">上架</a>
					{/if}
				</td>
			</tr>
			{/loop}
			<!--<tr>-->
			<!--<td colspan="5" style="text-align: right;"><a href="javascript:;" class="btn btn-success min-width js-batch js-off-shelves">下架</a></td>-->
			<!--<td><a href="javascript:;" class="btn btn-primary min-width js-batch js-on-shelves">上架</a></td>-->
			<!--</tr>-->
			{if !empty($coupon)}
			{if !empty($pager)}
			<tr>
				<td colspan="6">{$pager}</td>
			</tr>
			{/if}
			{else}
			<tr>
				<td colspan="6" style="text-align: center;"><span style="color:red;font-size: 18px;">暂无记录</span></td>
			</tr>
			{/if}
			</tbody>
		</table>
	</div>
</div>
{elseif $operation == 'off_shelves'}
<div class="main panel panel-default">
	<div class="panel-body table-responsive">
		<table class="table table-hover">
			<thead class="navbar-inner">
			<tr>
				<th style="width:5%;">ID</th>
				<th style="width:15%;">图片</th>
				<th style="width:35%;">优惠券名称</th>
				<th style="width:20%;">优惠券小标题</th>
				<th style="width:15%;">适用人群</th>
				<th class="text-center" style="width:10%;">操作</th>
			</tr>
			</thead>
			<tbody>
			{loop $list $coupon}
			<tr>
				<td>{$coupon['id']}</td>
				<td>
					<img width='45' height="45" src="{IMAGE_PIXEL}" data-url="{php echo tomedia($coupon['pic']);}" class="scrollLoading" onerror="this.src='{IMAGE_NOPIC_SMALL}'" />
				</td>
				<td>{$coupon['name']}</td>
				<td>{$coupon['subtitle']}</td>
				<td>{$coupon['person']}</td>
				<td class="text-right">
					<a href="{php echo web_url('goods/coupon', array('op' => 'post', 'id' => $coupon['id']))}" class="btn btn-default btn-sm" data-toggle="tooltip" data-placement="top" title="修改"><i class="fa fa-edit"></i></a>
					<a href="javascript:;" c-id="{$coupon['id']}" class="btn btn-default btn-sm js-delete" data-toggle="tooltip" data-placement="top" title="删除"><i class="fa fa-times"></i></a><br/>
					{if $coupon['isshow'] == 1}
					<a href="javascript:;" class="js-off-shelves-goods btn btn-default" goods-id="{$coupon['id']}">下架</a>&nbsp;&nbsp;
					{else}
					<a href="javascript:;" class="js-on-shelves-goods btn btn-default" goods-id="{$coupon['id']}">上架</a>
					{/if}
				</td>
			</tr>
			{/loop}
			<!--<tr>-->
			<!--<td colspan="5" style="text-align: right;"><a href="javascript:;" class="btn btn-success min-width js-batch js-off-shelves">下架</a></td>-->
			<!--<td><a href="javascript:;" class="btn btn-primary min-width js-batch js-on-shelves">上架</a></td>-->
			<!--</tr>-->
			{if !empty($coupon)}
			{if !empty($pager)}
			<tr>
				<td colspan="6">{$pager}</td>
			</tr>
			{/if}
			{else}
			<tr>
				<td colspan="6" style="text-align: center;"><span style="color:red;font-size: 18px;">暂无记录</span></td>
			</tr>
			{/if}
			</tbody>
		</table>
	</div>
</div>
{elseif $operation == 'post'}
<div class="main">
	<form action="" method="post" class="form-horizontal form" enctype="multipart/form-data" onsubmit='return formcheck()'>
		<input type="hidden" name="id" value="{$coupon['id']}" />
		<div class="panel panel-default">
			<div class="panel-heading">
				优惠券管理
			</div>
			<div class="panel-body">
				<!--<div class="form-group">-->
					<!--<label class="col-xs-12 col-sm-3 col-md-2 control-label">排序</label>-->
					<!--<div class="col-sm-9 col-xs-12">-->
						<!--<input type="text" name="id" class="form-control" value="{$coupon['id']}" />-->
					<!--</div>-->
				<!--</div>-->
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span style="color:red">*</span>优惠券名字</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" id='couponname' name="name" class="form-control" value="{$coupon['name']}" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券小标题</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="subtitle" class="form-control" value="{$coupon['subtitle']}" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">当前报名人数</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="applicant" class="form-control" value="{$coupon['applicant']}" />
					</div>
				</div>
                <div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">报名人数上限</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="up_limit" class="form-control" value="{$coupon['up_limit']}" />
					</div>
				</div>
                <div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">卡券最大使用</label>
					<div class="col-sm-9 col-xs-12">
						<div class="input-group">
							<input type="text" name="times" class="form-control" value="{$coupon['times']}" />
							<span class="input-group-addon">次</span>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">报名截止日期</label>
					<div class="col-sm-3 col-xs-4">
						{php echo tpl_form_field_date('deadline', $coupon['deadline'] , true)}
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">开始时间</label>
					<div class="col-sm-3 col-xs-4">
						{php echo tpl_form_field_date('start_time' , $coupon['start_time'] , true);}
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">结束时间</label>
					<div class="col-sm-3 col-xs-4">
						{php echo tpl_form_field_date('end_time' , $coupon['end_time'] , true);}
					</div>
				</div>
                <div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">开课时间:</label>
					<div class="col-sm-9 col-xs-12" style="position:relative">
						{php echo tpl_ueditor('class_time', $coupon['class_time']);}
					</div>
                </div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">优惠券图片</label>
					<div class="col-sm-9 col-xs-12">
						{php echo tpl_form_field_image('pic', $coupon['pic'])}
						<span class="help-block">建议尺寸大小640*400px</span>
					</div>

				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">适用人群</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="person" class="form-control" value="{$coupon['person']}" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">课程介绍:</label>
					<div class="col-sm-9 col-xs-12" style="position:relative">
						{php echo tpl_ueditor('course', $coupon['course']);}
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">上课地址</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="address" class="form-control" value="{$coupon['address']}" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">使用须知:</label>
					<div class="col-sm-9 col-xs-12" style="position:relative">
						{php echo tpl_ueditor('term', $coupon['term']);}
					</div>
				</div>
				<div class="form-group" style="display: none">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">核销员</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="salesman" class="form-control" value="{$coupon['salesman']}" />
					</div>
				</div>
			</div>
		</div>
		<div class="form-group col-sm-12">
			<input type="submit" name="submit" value="提交" onclick="formcheck();" class="btn btn-primary col-lg-1" />
			<input type="hidden" name="token" value="{$_W['token']}" />
		</div>
	</form>
</div>

<script language='javascript'>
	function formcheck() {
		if ($("#couponname").isEmpty()) {
			Tip.focus("couponname", "请填写优惠券名称!");
			return false;
		}
		return true;
	}
</script>
{/if}
<script>
    require(['bootstrap'],function($){
        $('.btn').hover(function(){
            $(this).tooltip('show');
        },function(){
            $(this).tooltip('hide');
        });
    });
    $(function(){
        $(".scrollLoading").scrollLoading();
        var $pop = null;
        $('.scrollLoading').hover(function(){
            var img = $(this).attr('src');
            var obj = this;
            var $pop = util.popover(obj, function($popover, obj){
                obj.$popover = $popover;
            }, '<div><img src="'+img+'" style="max-width:200px; max-height:200px;"></div>');
        }, function(){
            this.$popover.remove();
        });
        $('.js-delete').click(function(e) {
            e.stopPropagation();
            var cid = $(this).attr('c-id');
            var $this = $(this);
            util.nailConfirm(this, function(state) {
                if (!state) {
                    return;
                }
                $.post("{php echo web_url('goods/coupon/delete')}", {id : cid}, function(data) {
                    if(!data.errno){
                        $this.parent().parent().remove();
                    };
                    util.tips(data.message);
                }, 'json');
            });
        });

        $('.js-batch').click(function(e){
            e.stopPropagation();
            var goods_ids = [];
            var $checkboxes = $('.items:checkbox:checked');
            $checkboxes.each(function() {
                if (this.checked) {
                    goods_ids.push(this.value);
                };
            });

            if (goods_ids.length == 0) {
                util.tips('请选择要操作的项目!', 2000);
                return false;
            }
            var op = '';
            var html = '';
            if ($(this).hasClass('js-on-shelves')) {
                op = 'on_shelves';
                html = '确认上架?';
            } else if ($(this).hasClass('js-off-shelves')) {
                op = 'off_shelves';
                html = '确认下架?';
            }
            var $this = $(this);
            util.nailConfirm(this, function(state) {
                if(!state) return;
                $.post("{php echo web_url('goods/coupon/batch');}", {funcop : op, goods_ids : goods_ids}, function(data){
                    if(!data.errno){
                        $checkboxes.each(function() {
                            $(this).parent().parent().remove();
                        });
                    };
                    util.tips(data.message, 2000);
                }, 'json');
            }, {html: html});
        });

        $('.js-delete-goods, .js-off-shelves-goods, .js-on-shelves-goods, .js-remove-goods,.js-sellout-goods').click(function(e) {
            e.stopPropagation();
            var id = $(this).attr('goods-id');
            var _this = $(this).parent().parent();
            if ($(this).hasClass('js-on-shelves-goods')) {
                op = 'on_shelves';
                html = '确认上架?';
            } else if ($(this).hasClass('js-off-shelves-goods')) {
                op = 'off_shelves';
                html = '确认下架?';
            }
            util.nailConfirm(this, function(state) {
                if(!state) return;
                $.post("{php echo web_url('goods/coupon/single_op')}", {funcop : op, id : id}, function(data){
                    if(!data.errno){
                        _this.remove();
                    };
                    util.tips(data.message, 2000);
                }, 'json');
            }, {html: html});
        });

    });
</script>
{php include wl_template('common/footer');}